package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollectionUtil;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.exception.BusinessException;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.approve.api.IApl_typical_caseFeign;
import com.power.sifa.approve.model.Apl_typical_case;
import com.power.sifa.approve.model.pageInfo.Apl_typical_casePageInfo;
import com.power.sifa.approve.service.Apl_typical_caseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   典型案件收藏表控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_typical_caseController",tags={"典型案件收藏表"})
public class Apl_typical_caseController implements IApl_typical_caseFeign {

    @Resource
    private Apl_typical_caseService apl_typical_caseService;

    
    @Override
    @ApiOperation("查询典型案件收藏表分页列表")
    @SaCheckPermission("apl_typical_case_list")
    public ResponseDTO list(Apl_typical_casePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_typical_case> page = this.apl_typical_caseService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_typical_case apl_typical_case : page) {
                apl_typical_case.geneText();
                list.add(apl_typical_case);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看典型案件收藏表")
    @SaCheckPermission("apl_typical_case_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_typical_case apl_typical_case  = apl_typical_caseService.getById(id);
        if (apl_typical_case != null) {
            apl_typical_case.geneText();
        }
        return ResponseDTO.succData(apl_typical_case);
    }

    @Override
    @ApiOperation("删除典型案件收藏表")
    @SaCheckPermission("apl_typical_case_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_typical_caseService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改典型案件收藏表")
    @SaCheckPermission("apl_typical_case_save")
    public ResponseDTO save(Apl_typical_case apl_typical_case) {
        Assert.notNull(apl_typical_case, "apl_typical_case不能为空");
        apl_typical_caseService.saveOrUpdate(apl_typical_case);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除典型案件收藏表")
    @SaCheckPermission("apl_typical_case_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_typical_caseService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_typical_case_list")
    public void output(HttpServletResponse response, Apl_typical_casePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "典型案件收藏表";
        String[] heads = {"序号","申请人","被申请人","案由","标签","备注","创建人id","创建人名称","创建时间","案件id"};
        String[] fields = {"id","applyname","targetname","casecause","classify","remarks","createuserid","createusername","createtime","applyid"};
        Page<Apl_typical_case> page = this.apl_typical_caseService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "典型案件收藏表";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","申请人","被申请人","案由","标签","备注","创建人id","创建人名称","创建时间","案件id"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","applyname","targetname","casecause","classify","remarks","createuserid","createusername","createtime","applyid"};
        List<Apl_typical_case> list = (List<Apl_typical_case>)ExcelUtil.Import(fields, file, Apl_typical_case.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_typical_caseService.saveAll(list);
        }
        return ResponseDTO.succ();
	}


    /**
     * 条件查询
     * @param pageInfo
     * @return
     */
    @Override
    public ResponseDTO conditionQuery(Apl_typical_casePageInfo pageInfo) {

        List<Apl_typical_case> queryList = this.apl_typical_caseService.conditionQuery(pageInfo);
        PageDataResponse page = new PageDataResponse();
        if (CollectionUtil.isNotEmpty(queryList)) {
            Long total = Long.valueOf(pageInfo.getAllcount());
            int size = pageInfo.getPageSize() * pageInfo.getPageNumber();
            if (total.intValue() - size > 0) {
                page.setNextPage(pageInfo.getPageNumber() + 1);
            }
            page.setSize(pageInfo.getPageSize());
            page.setCurrent(pageInfo.getPageNumber());
            page.setTotal(total);
        } else {
            page.setSize(0);
            page.setCurrent(1);
            page.setTotal(0L);
        }
        page.setRecords(queryList);

        return ResponseDTO.succData(page);
    }

    /**
     * 通过applid和userid查询典型案例
     * @param applyid
     * @param userId
     * @return
     */
    @Override
    public ResponseDTO getTypicalDataByUserId(String applyid, String userId) {
        return ResponseDTO.succData(this.apl_typical_caseService.getTypicalDataByUserId(applyid,userId));
    }

    @Override
    public ResponseDTO pageTypicalCase(Apl_typical_casePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
//        try {
            return ResponseDTO.succData(this.apl_typical_caseService.pageTypicalCase(pageInfo));
//        } catch (BusinessException e) {
//            e.printStackTrace();
//            log.error(e.getMessage());
//            return ResponseDTO.wrap(500, "查询典型案例失败");
//        }
    }

}
